
/*
'===============================================================================
'  MP.LayerHelper.MP_OBS.BL.HrJobTitleManager
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.FactoryClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using MP.Core.Library;

namespace MP.LayerHelper.MP_OBS.BLL
{
	public class HrJobTitleManager : HrJobTitleManagerBase
	{
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public HrJobTitleManager()
		{
			// Nothing for now.
		}
		
		/// <summary>
		/// Method to create an instance of HrJobTitleManager
		/// </summary>
		/// <returns>An instant of HrJobTitleManager class</returns>
		public static HrJobTitleManager CreateInstant()
		{
			return new HrJobTitleManager();
		}

        public HrJobTitleEntity GetById(Guid Id)
        {
            EntityCollection<HrJobTitleEntity> items = SelectByIdLST(Id);
            if (items != null && items.Count > 0)
                return (items[0]);
            return null;
        }
        public HrJobTitleEntity GetByName(string name)
        {
            string strSQL = @"Select * From hrJobTitle Where IsDeleted=0 ";
            string tmpSQL = "";
            tmpSQL += " and dbo.[ufn_Compare](@JobTitleName, JobName)=1 ";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@JobTitleName", name);
            strSQL += tmpSQL;

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL, param);
            HrJobTitleEntity obj = null;
            while (dr.Read())
            {
                obj = new HrJobTitleEntity();
                obj.Id = (Guid)dr["Id"];
                if (!string.IsNullOrEmpty(dr["JobCode"].ToString().Trim()))
                    obj.JobCode = (string)dr["JobCode"];
                if (!string.IsNullOrEmpty(dr["JobName"].ToString().Trim()))
                    obj.JobName = (string)dr["JobName"];
                if (!string.IsNullOrEmpty(dr["Priority"].ToString().Trim()))
                    obj.Priority = (int)dr["Priority"];
                if (!string.IsNullOrEmpty(dr["Description"].ToString().Trim()))
                    obj.Description = (string)dr["Description"];
                if (!string.IsNullOrEmpty(dr["CreatedBy"].ToString().Trim()))
                    obj.CreatedBy = (string)dr["CreatedBy"];
                if (!string.IsNullOrEmpty(dr["CreatedDate"].ToString().Trim()))
                    obj.CreatedDate = (DateTime)dr["CreatedDate"];
                if (!string.IsNullOrEmpty(dr["UpdatedBy"].ToString().Trim()))
                    obj.UpdatedBy = (string)dr["UpdatedBy"];
                if (!string.IsNullOrEmpty(dr["UpdatedDate"].ToString().Trim()))
                    obj.UpdatedDate = (DateTime)dr["UpdatedDate"];
                obj.IsDeleted = (bool)dr["IsDeleted"];
            }
            return obj;
        }
        public HrJobTitleEntity GetByJobCode(string jobCode)
        {
            EntityCollection<HrJobTitleEntity> items = new EntityCollection<HrJobTitleEntity>();

            IPredicateExpression predicate = new PredicateExpression();
            predicate.Add(HrJobTitleFields.JobCode == jobCode);
            predicate.Add(HrJobTitleFields.IsDeleted == false);

            RelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(predicate);

            using (DataAccessAdapterBase adapter = new DataAccessAdapterFactory().CreateAdapter())
            {
                adapter.FetchEntityCollection(items, filter);
            }

            if (items != null && items.Count > 0)
                return items[0];
            return null;
        }

        public bool IsExistsByJobCode(string jobCode)
        {
            HrJobTitleEntity job = GetByJobCode(jobCode);
            if (job != null)
                return true;
            return false;
        }

        public DataSet GetAllJob()
        {
            string strSQL = "Select * " +
                            "From hrJobTitle " +
                            "Where IsDeleted=0 " +
                            "Order By Priority,JobName ";
            return SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL);
        }

        public void UpdatePriority(Guid Id, int priority)
        {
            string strSQL = "Update hrJobTitle " +
                            "Set Priority=@Priority " +
                            "Where Id=@Id";
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@Priority", priority);
            param[1] = new SqlParameter("@Id", Id);
            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL, param);
        }

        public DataTable GetByDepart(Guid departId)
        {
            string strSQL = " SELECT * " +
                            " FROM hrJobTitle j INNER JOIN hrHumanOrganize o ON j.ID = o.JobId " +
                            " WHERE j.IsDeleted = 0 " +
                                " AND o.DepartId = '" + departId.ToString() + "' ";
            return SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL);
        }
	}
}
